package com.wjw.controller;

import com.wjw.entity.Business;
import com.wjw.entity.Users;
import com.wjw.service.BusinessService;
import com.wjw.service.UsersService;
import com.wjw.utils.CookieUtil;
import com.wjw.vo.UsersVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * (users)表控制层
 *
 * @author xxxxx
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UsersController {

    @Autowired
    private UsersService usersService;

    @Autowired
    private BusinessService businessService;

    /**
     * @param userMap
     * @param req
     * @param res
     * @return
     */
    @RequestMapping(value = "/register")
    public Map userRegister(@RequestBody Map<String, String> userMap, HttpServletRequest req, HttpServletResponse res) {
        String userName = userMap.get("userName");
        String password = userMap.get("password");
        HashMap map = new HashMap<String, String>();
        if (usersService.findUserByName(userName) > 0) {
            map.put("code", "004");
            map.put("msg", "用户名已存在，不能注册");
        } else {
            Users user = usersService.registerUser(userName, password);
            map.put("code", "001");
            map.put("msg", "注册成功");
            String ticket = UUID.randomUUID().toString();
            HttpSession session = req.getSession();
            session.setAttribute(ticket, user);
            CookieUtil.setCookie(req, res, "userTicket", ticket, -1, false);
            log.info("用户" + user.getUsername() + "注册成功:" + ticket);
        }
        return map;
    }

    /**
     * @param userMap
     * @return
     */
    @RequestMapping(value = "/findUserName")
    public Map findByName(@RequestBody Map<String, String> userMap) {
        String userName = userMap.get("userName");
        HashMap map = new HashMap<String, String>();
        if (usersService.findUserByName(userName) > 0) {
            map.put("code", "004");
            map.put("msg", "用户名已存在，不能注册");
        } else {
            map.put("code", "001");
            map.put("msg", "可以注册");
        }
        return map;
    }

    /**
     * @param userMap
     * @param req
     * @param res
     * @return
     */
    @PostMapping(value = "/login")
    public Map userLogin(@RequestBody Map<String, String> userMap, HttpServletRequest req, HttpServletResponse res) {
        String userName = userMap.get("userName");
        String password = userMap.get("password");
        HashMap map = new HashMap<String, String>();
        Users users = usersService.login(userName, password);
        UsersVo user = new UsersVo();
        BeanUtils.copyProperties(users, user);
        Business byUserId = businessService.getByUserId(users.getUserId());
        if (users != null) {
            if (byUserId != null) {
                user.setBusinessId(byUserId.getBusinessId());
                user.setBusinessName(byUserId.getBusinessName());
                user.setBusinessPhone(byUserId.getBusinessPhone());
            }
            map.put("code", "001");
            map.put("msg", "登录成功");
            map.put("user", user);
            String ticket = UUID.randomUUID().toString();
            HttpSession session = req.getSession();
            session.setAttribute(ticket, users);
            CookieUtil.setCookie(req, res, "userTicket", ticket, -1, false);
            log.info("用户" + users.getUsername() + "登录:" + ticket);
        } else {
            map.put("code", "004");
            map.put("msg", "登录失败");
        }
        return map;
    }

    /**
     * @param user
     * @return
     */
    @RequestMapping(value = "/updateUser")
    public Map updateUser(Users user) {
        HashMap map = new HashMap<String, String>();
        usersService.updateUser(user);
        map.put("code", "001");
        map.put("msg", "修改成功");
        return map;
    }

    @RequestMapping(value = "/logout")
    public void userLogout(@RequestBody Map<String, String> map, HttpServletRequest req) {
        String userTicket = CookieUtil.getCookieValue(req, "userTicket", false);
        req.getSession().removeAttribute(userTicket);
        log.info("用户" + map.get("username") + "退出登录");
    }

    /**
     * 个人信息
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/user-info")
    @ResponseBody
    public Map userInfo(@RequestBody Map<String, Integer> map1) {
        HashMap map = new HashMap<String, String>();
        //Integer id = Integer.valueOf(userId);
        Integer userId = map1.get("userId");
        Users users = usersService.userInfo(userId);
        UsersVo user = new UsersVo();
        if (users == null){
            map.put("code", "004");
            map.put("msg", "获取信息失败");
        }
        BeanUtils.copyProperties(users, user);
        Business byUserId = businessService.getByUserId(users.getUserId());
        if (byUserId != null) {
            user.setBusinessId(byUserId.getBusinessId());
            user.setBusinessName(byUserId.getBusinessName());
            user.setBusinessPhone(byUserId.getBusinessPhone());
        }
        map.put("code", "001");
        map.put("msg", "获取信息成功");
        map.put("user", user);
        return map;
    }

    /**
     * 修改
     * @param vo
     * @return
     */

    @RequestMapping(value = "/modify")
    @ResponseBody
    public Boolean modify(@RequestBody UsersVo vo) {
        if (vo == null) {
            return false;
        }
        Boolean modify = usersService.modify(vo);
        if (vo.getBusinessId() == null) {
            return modify;
        } else {
            Boolean aBoolean = businessService.modify(vo);
            if (modify && aBoolean) {
                return true;
            }
            return false;
        }

    }

}
